//
//  ChatListTableCell.m
//  ChatDemo
//
//  Created by 度周末网络-王腾 on 15/12/21.
//  Copyright © 2015年 imac. All rights reserved.
//

#import "ChatListTableCell.h"

@implementation ChatListTableCell

- (void)awakeFromNib {
    self.unreadCount.layer.cornerRadius = 10.0f;
    self.unreadCount.layer.masksToBounds=YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


-(void)setModel:(EaseConversationModel *)conversationModel{
    self.img.image = conversationModel.avatarImage;
    self.chatMessage.text = conversationModel.title;
    
    if (conversationModel.conversation.unreadMessagesCount == 0) {
        self.unreadCount.hidden = YES;
    }else{
        self.unreadCount.text = @(conversationModel.conversation.unreadMessagesCount).stringValue;
        self.unreadCount.hidden = NO;
    }
    
    self.lastMessageLabel.text = [self latestMessageTitleForConversationModel:conversationModel];
}


- (NSString *)latestMessageTitleForConversationModel:(EaseConversationModel *)conversationModel
{
    NSString *latestMessageTitle = @"";
    EMMessage *lastMessage = [conversationModel.conversation latestMessage];
    if (lastMessage) {
        id<IEMMessageBody> messageBody = lastMessage.messageBodies.lastObject;
        switch (messageBody.messageBodyType) {
            case eMessageBodyType_Image:{
                latestMessageTitle = NSLocalizedString(@"message.image1", @"[image]");
            } break;
            case eMessageBodyType_Text:{
                // 表情映射。
                NSString *didReceiveText = [EaseConvertToCommonEmoticonsHelper
                                            convertToSystemEmoticons:((EMTextMessageBody *)messageBody).text];
                latestMessageTitle = didReceiveText;
            } break;
            case eMessageBodyType_Voice:{
                latestMessageTitle = NSLocalizedString(@"message.voice1", @"[voice]");
            } break;
            case eMessageBodyType_Location: {
                latestMessageTitle = NSLocalizedString(@"message.location1", @"[location]");
            } break;
            case eMessageBodyType_Video: {
                latestMessageTitle = NSLocalizedString(@"message.video1", @"[video]");
            } break;
            case eMessageBodyType_File: {
                latestMessageTitle = NSLocalizedString(@"message.file1", @"[file]");
            } break;
            default: {
            } break;
        }
    }
    
    return latestMessageTitle;
}

@end
